import { defineStore } from "pinia";

const useAccountStore = defineStore("account", {
    state: () => ({
        userMsg: {}
    }),
    actions: {
        setUserMsg(newValue: object) {
            this.userMsg = newValue
            this.saveUserMsgToLocalStorage()
        },
        saveUserMsgToLocalStorage() {

            localStorage.setItem('userMsg', JSON.stringify(this.userMsg))
        },
        getUserMsgFromLocalStorage() {

            const userMsgStr = localStorage.getItem('userMsg')
            if (!userMsgStr) {
                this.userMsg = {}
                return
            }
            try {
                const userMsg = JSON.parse(userMsgStr)
                this.userMsg = userMsg
            } catch (error) {
                console.error('解析 localStorage 数据失败:', error)
                this.userMsg = {}
            }
        },
        clearUserMsg() {
            this.userMsg = {}
        }
    }
})

export default useAccountStore;